<?php

namespace App\Logics;


use App\Models\CourseSign;
use App\Models\CourseStudent;
use App\Models\User;
use App\Service\Base\Logic;

class CourseStudentLogic extends Logic
{

    // 课程学员
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseStudent';
    }

    // 列表
    // 添加
    // 修改
    // 软删除

    // 列表查询
    public function onLists($myUid = 0)
    {
        $option = $this->option();
        $page   = $option->getData('page', 1, 'intval');
        $rows   = $option->getData('rows', 15, 'intval');
        $isPage = $option->getData('is_count', 0, 'intval');  // 是否需要翻页
        $start  = ($page - 1) * $rows;

        // 模型 和 查询
        $where   = [];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $s_id    = $option->getData('student_id', 0, 'intval');
        $name    = $option->getData('name', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($myUid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $myUid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if($name){
            $bang    = false;
            $where[] = ['name', 'like', "%".$name."%"];
        }

        // 模型
        $model  = new CourseStudent();
        $KCache = $model->cacheKey('lists', $where);
        $result = $model->cacheClass()->getCache($KCache);
        // 检查是否已经绑定
        if(!empty($result)){
            if($isPage){
                if(!empty($result['list'])){
                    $model->checkBangList($result['list'], $KCache);
                }
            } else{
                $model->checkBangList($result, $KCache);
            }
        }
        if(empty($result)){
            if($isPage){
                $result = $model->listPage($where, $page, $rows, "id DESC");
            } else{
                $result = $model->getList($where, $rows, "id DESC", $start);
                $result = is_object($result) ? $result->toArray() : $result;
            }
            /*if($where != false){
                $model->addCacheList($result, $KCache, $result, 'list');
            }*/
        }

        return $result;
    }

    //   上课时间
    public function onShow()
    {
        $option = $this->option();
        $c_id   = $option->getData('course_id', 0, 'intval');
        $_id    = $option->getData('id', 0, 'intval');  // 如果是学员，看自己的课程信息

        // 课程时间
        $timesDb = new CourseStudent();
        $times   = $timesDb->bangField(['course_id'])->oneFind(['id' => $_id]);
        if(empty($times)){
            return $this->_returnFail($timesDb->getError());
        }

        return $times;
    }


    // 直接删除
    public function onDelete($myUid)
    {
        $opt = $this->option();
        $_id = $opt->getData('id', 0, 'intval');
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where  = ['id' => $_id, 'uid' => $myUid];
        $model  = new CourseStudent();
        $result = $model->isCache(true)->oneDelete($where);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }


    // 直接删除
    public function batchDelete($myUid)
    {
        $opt  = $this->option();
        $_ids = $opt->getData('ids', [], 'is_array');
        if(empty($_ids)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = ['uid' => $myUid];
        $model = new CourseStudent();
        $list  = $model->whereIn('id', $_ids)->where($where)->get();  //->recycle()
        if($list->isEmpty()){
            return $this->_returnFail('你要删除的数据不存在！！');
        }

        // 缓存操作
        $model->deleteCacheList($list);
        $res = $model->whereIn('id', $_ids)->where($where)->delete();  //->recycle()

        return $res;
    }


    // 批量添加
    public function batchAdd($myUid)
    {
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $option     = $this->option();
        $course_id  = $option->getData("course_id", null, "intval");
        $studentIds = $option->getData("student_id", [], "is_array");
        if(empty($studentIds)){
            return $this->_returnFail('请选择需要添加的用户！');
        }
        if(empty($course_id)){
            return $this->_returnFail('请选择需要课程！');
        }

        $all = [];
        foreach($studentIds as $_stuId){
            $all[] = ['uid' => $myUid, 'course_id' => $course_id, 'student_id' => $_stuId, 'ctime' => LARAVEL_TIME, 'mtime' => LARAVEL_TIME,];
        }

        // 执行  删除以前的
        $model = new CourseStudent();
        $where = ['uid' => $myUid, 'course_id' => $course_id,];
        $lists = $model->where($where)->get()->toArray();
        if($lists){
            $model->where($where)->delete();
        }


        // 添加
        $result = $model->insert($all);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 缓存操作
        $model->deleteCacheList($lists);
        $model->deleteCacheList($all);

        return $result;
    }

    // 改变
    public function onEdit($myUid)
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定修改的ID编号！');
        }

        $opt = $this->option();
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $auth_id = $user['auth_id'];
        $data    = [
            //'id'          => $opt->getData("id", null, "intval"),
            //'uid'         => $myUid,
            'course_id'   => $opt->getData("course_id", null, "intval"),
            'student_id'  => $opt->getData("student_id", null, "intval"),
            'course_uid'  => $opt->getData("course_uid", null, "intval"),
            'person_name' => $opt->getData("person_name", null, "trim"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'status'      => $opt->getData("status", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $where = ['id' => $_id, 'uid' => $myUid];
        $model = new CourseStudent();
        $data  = $model->checkData($data, $where);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'edit');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneEdit($where, $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 树状
    public function onAdd($myUid)
    {
        $opt = $this->option();
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $myUid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你不是认证用户！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }


        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'         => $myUid,
            'course_id'   => $opt->getData("course_id", null, "intval"),
            'student_id'  => $opt->getData("student_id", null, "intval"),
            'course_uid'  => $opt->getData("course_uid", null, "intval"),
            'person_name' => $opt->getData("person_name", null, "trim"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'status'      => $opt->getData("status", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];


        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 组合数据
        $model = new CourseStudent();
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail('请添加需要保存的数据！');
        }

        // 验证
        $result = $model->checkRules($data, 'add');
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 保存
        $result = $model->oneAdd($data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

}


